

enum motor {
    //% block="M1"
    1,
    //% block="M2"
    2,
    //% block="M3"
    3,
    //% block="M4"
    4,
    //% block="M5"
    5,
    //% block="M6"
    6
}

enum direction {
    //% block="正转"
    1,
    //% block="反转"
    2,
    //% block="停止"
    3
    
}



//% color="#FFCC66" iconWidth=60 iconHeight=50
namespace control02 {
    
     //% block=" What [MOT] is [SPEED] at what [MOVING] ?" blockType="command"
     //%  MOT.shadow="dropdown" MOT.options="motor" MOT.defl="motor.1"
     //%  SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=255 SPEED.defl=0
     //%  MOVING.shadow="dropdown" MOVING.options="direction" MOVING.defl="direction.1"
     
     export function motorforward(parameter: any, block: any) {
        if(Generator.board === 'arduinonano'){
            let mot = parameter.MOT.code
            let speed = parameter.SPEED.code
            let moving = parameter.MOVING.code
          
            let code1= "";
            Generator.addInclude("motorcontrol01", "#include <motorcontrol.h>");
            // Generator.addSetup(`set_motor();`);
            Generator.addSetup("motorcontrol01","set_motor();",true);
            

            if(moving ==='1')
            {
                if (mot === '1') {
                    code1 = `set_motor_pwm(${speed}, MOT_A1_PIN ,MOT_A2_PIN );`;
                } else if (mot === '2') {
                    code1 = `set_motor_pwm(${speed}, MOT_B1_PIN ,MOT_B2_PIN );`;
                } else if (mot === '3') {
                    code1 = `set_motor_pwm(${speed}, MOT_C1_PIN ,MOT_C2_PIN );`;
                } else if (mot === '4') {
                    code1 = `set_motor_pwm(${speed}, MOT_D1_PIN ,MOT_D2_PIN );`;
                } else if (mot === '5') {
                    code1 = `set_motor_pwm(${speed}, MOT_E1_PIN ,MOT_E2_PIN );`;
                } else if (mot === '6') {
                    code1 = `set_motor_pwm(${speed}, MOT_F1_PIN ,MOT_F2_PIN );`;
                } 
    
                Generator.addCode(code1);

            }
            else if (moving === '2')
            {
                let reversedSpeed = speed * -1;
                if (mot === '1') {
                    code1 = `set_motor_pwm(${reversedSpeed}, MOT_A1_PIN ,MOT_A2_PIN );`;
                } else if (mot === '2') {
                    code1 = `set_motor_pwm(${reversedSpeed}, MOT_B1_PIN ,MOT_B2_PIN );`;
                } else if (mot === '3') {
                    code1 = `set_motor_pwm(${reversedSpeed}, MOT_C1_PIN ,MOT_C2_PIN );`;
                } else if (mot === '4') {
                    code1 = `set_motor_pwm(${reversedSpeed}, MOT_D1_PIN ,MOT_D2_PIN );`;
                } else if (mot === '5') {
                    code1 = `set_motor_pwm(${reversedSpeed}, MOT_E1_PIN ,MOT_E2_PIN );`;
                } else if (mot === '6') {
                    code1 = `set_motor_pwm(${reversedSpeed}, MOT_F1_PIN ,MOT_F2_PIN );`;
                } 
                Generator.addCode(code1);
    
            }
            else if(moving === '3')
            {
                if (mot === '1') {
                    code1 = `set_motor_pwm(0, MOT_A1_PIN ,MOT_A2_PIN );`;
                } else if (mot === '2') {
                    code1 = `set_motor_pwm(0, MOT_B1_PIN ,MOT_B2_PIN );`;
                } else if (mot === '3') {
                    code1 = `set_motor_pwm(0, MOT_C1_PIN ,MOT_C2_PIN );`;
                } else if (mot === '4') {
                    code1 = `set_motor_pwm(0, MOT_D1_PIN ,MOT_D2_PIN );`;
                } else if (mot === '5') {
                    code1 = `set_motor_pwm(0, MOT_E1_PIN ,MOT_E2_PIN );`;
                } else if (mot === '6') {
                    code1 = `set_motor_pwm(0, MOT_F1_PIN ,MOT_F2_PIN );`;
                } 
                Generator.addCode(code1);

            }

           

    }
}



}
